import json
import time
from pathlib import Path
from typing import List

from ScrapyObject.spiders.utils.aa import read_json_file, VideoBean, spiderInfo


class RoomListBean:
    timeStamp = 0
    data = []

    def __init__(self, timeStamp, data):
        self.timeStamp = timeStamp
        self.data = data


class RoomListItemBean:
    videoTag = ""
    videoUrl = ""

    def __init__(self, videoTag, videoUrl):
        self.videoTag = videoTag
        self.videoUrl = videoUrl


def list_files_with_pathlib(folder_path: str or Path, recursive: bool = True) -> List[Path]:
    """
    获取指定文件夹下的所有文件。

    :param folder_path: 文件夹路径（字符串或 Path 对象）
    :param recursive: 是否递归遍历子目录，默认为 True
    :return: 包含所有文件 Path 对象的列表
    """
    folder = Path(folder_path)

    if not folder.exists():
        raise FileNotFoundError(f"路径不存在: {folder}")

    if not folder.is_dir():
        raise NotADirectoryError(f"指定的路径不是文件夹: {folder}")

    # 根据是否递归选择方法
    pattern = '**/*' if recursive else '*'

    # 获取所有匹配的项目，并过滤出文件
    files = [item for item in folder.glob(pattern) if item.is_file()]

    return sorted(files)  # 按名称排序返回，便于查看


# 3把最终结果分解成几份
if __name__ == '__main__':
    print('开始')
    timestamp = int(time.time())
    alls_1 = list_files_with_pathlib('D:\\8\\tag\\')
    videoList = []
    temp_index = 0
    for index, all_1 in enumerate(alls_1):
        jsonStr = read_json_file(all_1)
        # print("原始数据一共: " + str(len(jsonStr)))
        if len(videoList) < 50000 and index < len(alls_1) - 1:
            for i in jsonStr:
                video = VideoBean()
                video.id = 0
                video.name = i['name']
                video.url = i['url']
                video.tags = i['tags']
                video.pUrl = i['pUrl']
                video.vUrl = i['vUrl']
                videoList.append(video)
        else:
            videoFinalList = []
            for i in videoList:
                tinydict = {'id': 0, 'name': i.name, 'url': i.url, 'tags': i.tags, 'pUrl': i.pUrl, 'vUrl': i.vUrl}
                videoFinalList.append(tinydict)
            # 将JSON数组转换为字符串
            json_string = json.dumps(videoFinalList, ensure_ascii=False)
            # 将字符串写入txt文件
            with open('D:\\8\\final\\'+spiderInfo.name + str(temp_index) + '.json', 'w', encoding='utf-8') as file:
                file.write(json_string)
            temp_index += 1
            videoList = []
        jsonStr.clear()

    endTime = int(time.time())
    print(str(endTime - timestamp))
    print('结束')
